﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Org.BouncyCastle.Crypto;
using XiAnAirport.BUS;
using XiAnAirport.DAL;
using XiAnAirport.ElevatorWebSocket;
using XiAnAirport.Models;

namespace XiAnAirport.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class RepairWorkOrderController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        JwtToken _jwttoken = new JwtToken();
        IHttpContextAccessor _access;
        IWebSocketManage _webSocketManage;
        private readonly IHostingEnvironment _hostingEnvironment;
        public RepairWorkOrderController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access, IWebSocketManage webSocketManage, IHostingEnvironment hostingEnvironment
         )
        {
            _jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
            _webSocketManage = webSocketManage;
            _hostingEnvironment = hostingEnvironment;
        }

        /// <summary>
        /// 新增走修工单
        /// </summary>
        /// <param name="workorderinfo">工单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddWorkOrder")]
        public ResponseMessage AddWorkOrder(RepairWorkOrderInfo workorderinfo)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                workorderinfo.creator = HttpContext.Session.GetString("UserName").ToString();
                if (string.IsNullOrEmpty(workorderinfo.acceptanceTime))
                {
                    workorderinfo.acceptanceTime = "null";
                }
                else
                {
                    workorderinfo.acceptanceTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.acceptanceTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.arriveTime))
                {
                    workorderinfo.arriveTime = "null";
                }
                else
                {
                    workorderinfo.arriveTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.arriveTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.completeTime))
                {
                    workorderinfo.completeTime = "null";
                }
                else
                {
                    workorderinfo.completeTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.completeTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.preparationTime))
                {
                    workorderinfo.preparationTime = "null";
                }
                else
                {
                    workorderinfo.preparationTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.preparationTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.receiveTime))
                {
                    workorderinfo.receiveTime = "null";
                }
                else
                {
                    workorderinfo.receiveTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.receiveTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.reportTime))
                {
                    workorderinfo.reportTime = "null";
                }
                else
                {
                    workorderinfo.reportTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.reportTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.maintenanceTime))
                {
                    workorderinfo.maintenanceTime = "null";
                }
                else
                {
                    workorderinfo.maintenanceTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.maintenanceTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                ElevatorStateBUS elevatorStateBUS = new ElevatorStateBUS();
                message = BUS.AddWorkOrder(workorderinfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增维修工单" + workorderinfo.workorderNO;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "新增走修工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增走修工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 修改工单
        /// </summary>
        /// <param name="workorderinfo">工单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("EditWorkOrder")]
        public ResponseMessage EditWorkOrder(RepairWorkOrderInfo workorderinfo)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                if (string.IsNullOrEmpty(workorderinfo.acceptanceTime))
                {
                    workorderinfo.acceptanceTime = "null";
                }
                else
                {
                    workorderinfo.acceptanceTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.acceptanceTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.arriveTime))
                {
                    workorderinfo.arriveTime = "null";
                }
                else
                {
                    workorderinfo.arriveTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.arriveTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.completeTime))
                {
                    workorderinfo.completeTime = "null";
                }
                else
                {
                    workorderinfo.completeTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.completeTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.preparationTime))
                {
                    workorderinfo.preparationTime = "null";
                }
                else
                {
                    workorderinfo.preparationTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.preparationTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.receiveTime))
                {
                    workorderinfo.receiveTime = "null";
                }
                else
                {
                    workorderinfo.receiveTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.receiveTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.reportTime))
                {
                    workorderinfo.reportTime = "null";
                }
                else
                {
                    workorderinfo.reportTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.reportTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (string.IsNullOrEmpty(workorderinfo.maintenanceTime))
                {
                    workorderinfo.maintenanceTime = "null";
                }
                else
                {
                    workorderinfo.maintenanceTime = string.Format("'{0}'", Convert.ToDateTime(workorderinfo.maintenanceTime).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                message = BUS.EditWorkOrder(workorderinfo);
                if (message.errcode == 0)
                {
                    if (workorderinfo.workorderStateID == 4)
                    {
                        RepairWorkOrderDAL da = new RepairWorkOrderDAL();
                        da.UpdateFaultTime(workorderinfo.id);
                    }
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改维修工单" + workorderinfo.workorderNO;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改走修工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改走修工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询所有维保员
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryAllRepairman")]
        public ResponseMessage QueryAllRepairman()
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                message = BUS.QueryAllRepairman();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询维保员失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询维保员";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除走修工单
        /// </summary>
        /// <param name="workorderinfo">工单ID</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelWorkOrder")]
        public ResponseMessage DelWorkOrder(RepairWorkOrderInfo workorderinfo)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                message = BUS.DelWorkOrder(workorderinfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除维修工单" + workorderinfo.workorderNO;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除走修工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除走修工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入走修工单
        /// </summary>
        /// <param name="workorderinfo">工单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadWorkOrder")]
        public ResponseMessage LoadWorkOrder(RepairWorkOrderInfo workorderinfo)
        {
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.LoadWorkOrder(workorderinfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入走修工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入走修工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询走修工单
        /// </summary>
        /// <param name="workorderinfo">工单信息</param>
        /// 
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryWorkOrderList")]
        public ResponseMessage QueryWorkOrderList(RepairWorkOrderInfo workorderinfo)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                message = BUS.QueryWorkOrder(workorderinfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询走修工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询走修工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询历史走修工单
        /// </summary>
        /// <param name="workorderinfo">工单信息</param>
        /// 
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryWorkOrderHistroyList")]
        public ResponseMessage QueryWorkOrderHistroyList(RepairWorkOrderInfo workorderinfo)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                message = BUS.QueryWorkOrderHistory(workorderinfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询走修历史工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询走修历史工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询故障历史
        /// </summary>
        /// <param name="faultReport">信息</param>
        /// 
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryFaultHistroyList")]
        public ResponseMessage QueryFaultHistroyList(FaultReportInfo faultReport)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                message = BUS.QueryFaultHistory(faultReport);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询故障历史失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询故障历史";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 导出维修工单
        /// </summary>
        /// <param name="repairWorkOrder">维修信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportRepairWorkOrderInfo")]
        public ResponseMessage ExportRepairWorkOrderInfo(RepairWorkOrderInfo repairWorkOrder)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                RepairWorkOrderBUS bus = new RepairWorkOrderBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportRepair(repairWorkOrder, sWebRootFolder);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "导出维修工单";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出维修工单";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 生成工单号
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GenerateRepairWorkOrderNo")]
        public ResponseMessage GenerateInspectionNo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                RepairWorkOrderBUS bus = new RepairWorkOrderBUS();
                message = bus.GenerateRepairWorkOrderNo();
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "生成工单号异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "生成走修工单号";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }


        /// <summary>
        /// 导出维修历史
        /// </summary>
        /// <param name="repairWorkOrder">维修信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportRepairWorkOrderInfoHistory")]
        public ResponseMessage ExportRepairWorkOrderInfoHistory(RepairWorkOrderInfo repairWorkOrder)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                RepairWorkOrderBUS bus = new RepairWorkOrderBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportRepairHistory(repairWorkOrder, sWebRootFolder);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "导出维修历史";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出维修历史";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }


        /// <summary>
        /// 导出故障历史
        /// </summary>
        /// <param name="repairWorkOrder">维修信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportQueryFaultHistroy")]
        public ResponseMessage ExportQueryFaultHistroy(FaultReportInfo faultReportr)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                RepairWorkOrderBUS bus = new RepairWorkOrderBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportFaultHistroy(faultReportr, sWebRootFolder);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "导出故障历史";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出故障历史";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 批量删除走修工单
        /// </summary>
        /// <param name="workorderinfo">工单ID</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelWorkOrder")]
        public ResponseMessage BatchDelWorkOrder(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderBUS BUS = new RepairWorkOrderBUS();
            try
            {
                message = BUS.BatchDelWorkOrder(ids);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除走修工单" + ids;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除走修工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除走修工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
